package com.lwm.lambda;


import com.lwm.entity.Employee;
import com.lwm.jiekou.MyFun;
import org.junit.Test;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * @author liweimin
 */
public class LambdaTest {
    @Test
    public void test01() {
        Runnable runnable = () -> System.out.println("hello");
        runnable.run();
    }

    @Test
    public void test02() {
        Consumer<String> consumer = (x) -> System.out.println(x);
        consumer.accept("wode");
    }

    @Test
    public void test03() {
        Comparator<Integer> comparator = (x, y) -> {
            return Integer.compare(x, y);
        };
        System.out.println(comparator.compare(21, 45));
    }

    @Test
    public void test04() {
        System.out.println(operation(8, x -> x = 9 + x));
    }

    @Test
    public void test05() {
        List<Employee> employeeList = Arrays.asList(
                new Employee("张三", 23, 6433),
                new Employee("张三1", 33, 4433),
                new Employee("张三2", 63, 65333),
                new Employee("张三3", 43, 5333),
                new Employee("张三4", 36, 4233)
        );
        Collections.sort(employeeList, (x, y) -> {
            if (x.getAge() > y.getAge())
                return x.getName().compareTo(y.getName());
            return Integer.compare(x.getAge(), y.getAge());
        });
        employeeList.forEach(System.out::println);
    }

    @Test
    public void ConsumerTest() {
        happy(122, m -> System.out.println(m * m));
    }

    public void happy(double money, Consumer<Double> con) {
        con.accept(money);
    }

    @Test
    public void FunctionTest() {
        System.out.println(stringHandler("\t\t\tHello World", str -> str.trim()));
        System.out.println(stringHandler("dsafsadfsdfsdfsdfsdfs", str -> str.toUpperCase()));
    }

    public String stringHandler(String s, Function<String, String> function) {
        return function.apply(s);
    }

    @Test
    public void Predicate() {
        List<String> list = Arrays.asList("sd", "sdasd", "fsd", "ffsdfs", "qwetw", "sfsfs");
        System.out.println(filterStr(list, (e) -> e.length() > 3));
    }

    public List<String> filterStr(List<String> list, Predicate<String> predicate) {
        List<String> list1 = new ArrayList<>();
        for (String e : list) {
            if (predicate.test(e)) {
                list1.add(e);
            }
        }
        return list1;
    }

    @Test
    public void SupplierTest() {
        getIntList(20, () -> (int) (Math.random() * 100)).forEach(System.out::println);
    }

    public List<Integer> getIntList(int num, Supplier<Integer> supplier) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            Integer integer = supplier.get();
            list.add(integer);
        }
        return list;
    }

    public Integer operation(Integer o, MyFun myFun) {
        return myFun.getValue(o);
    }
}
